package com.jiaai.cloud.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiaai.cloud.base.entity.RestResultGenerator;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.weixin.common.MD5;
import com.jiaai.cloud.base.weixin.util.WeiXinUtil;
import com.jiaai.cloud.wx.entity.TAdmin;
import com.jiaai.cloud.wx.entity.TEmp;
import com.jiaai.cloud.wx.service.ITAdminService;
import com.jiaai.cloud.wx.service.ITEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2017/8/30.
 */
@Controller
@RequestMapping("/api/v1")
public class LoginController {
    @Autowired
    private ITEmpService itEmpService;
//    @Autowired
//    private ITAdminService itAdminService;
//
//    //todo
//    /**
//     * 后台管理员登录页面
//     * @param tAdmin 不可空
//     * @param session
//     * @return
//     * @throws ServiceException
//     */
//    @RequestMapping(value = "/login",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
//    @ResponseBody
//    public Object backLogin(TAdmin tAdmin, HttpSession session) throws ServiceException {
//        TAdmin tAdmin1 = itAdminService.selectOne(new EntityWrapper<TAdmin>().eq("adminname", tAdmin.getAdminname())
//                .eq("adminpassword", MD5.MD5Encode(tAdmin.getAdminpassword())));
//        try {
//            if (tAdmin1 != null){
//                tAdmin1.setAdminpassword("");
//                session.setAttribute("LOGIN_ADMIN", tAdmin1);
//                return RestResultGenerator.genSuccessResult(tAdmin1);
//            }
//            return RestResultGenerator.genSuccessResult("用户名或密码错误");
//        } catch (Exception e){
//            throw new ServiceException("登录失败", e.getMessage());
//        }
//    }

    //todo
    /**
     * 用户关注小程序后, 登录****没有该用户将跳转到填写邀请码的页面
     * @param code
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object login(String code) throws Exception {
        Object session = WeiXinUtil.getAppSessionKey("wxb92eecad4e1e5e44", "6fa67444bcbc9c557ef9082d95c1a5db", code).get("session");
        JSONObject jsonObject = (JSONObject) session;
        String openid = jsonObject.get("openid").toString();
        TEmp tEmp = itEmpService.selectOne(new EntityWrapper<TEmp>().isNotNull("groupid").eq("openid", openid));

        try {
            if (tEmp == null){
                throw new Exception("没有用户");
            } else {
                return RestResultGenerator.genSuccessResult(tEmp);
            }
        } catch (Exception e){
            throw new ServiceException("登录失败", e.getMessage());
        }
    }
}
